package common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BlockInfo {
    private int blockNO; // 存储模块内部编号
    private String fileName; // 对应文件名
    private int status; // 对应StateCode中的blockCode
    private List<DNInfo> locations; // 存储有该block的datanode位置
    private int validLen; // 有效长度
    private long CRC;
    public int getValidLen() {
        return validLen;
    }

    public BlockInfo(int blockNO, String fileName, int status, List<DNInfo> locations, int validLen, long CRC) {
        this.blockNO = blockNO;
        this.fileName = fileName;
        this.status = status;
        this.locations = locations;
        this.validLen = validLen;
        this.CRC = CRC;
    }

    @Override
    public String toString() {
        return  blockNO +","+
                fileName + ","+
                status +","+
                validLen +","+ CRC;
    }

    public int getBlockNO() {
        return blockNO;
    }

    public void setBlockNO(int blockNO) {
        this.blockNO = blockNO;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public long getCRC() {
        return CRC;
    }

    public void setCRC(long CRC) {
        this.CRC = CRC;
    }

    public void setValidLen(int validLen) {
        this.validLen = validLen;
    }

    public void setLocations(List<DNInfo> locations) {
        this.locations = locations;
    }

    public BlockInfo(String fileName, int blockNo, int validLen) {
        this.fileName = fileName;
        this.validLen = validLen;
        locations = new ArrayList<>();
        status = StateCode.blockEmpty;
        this.blockNO = blockNo;
    }

    /**
     * 增加该块的存储位置
     * @param IP
     * @param PORT
     */
    public void appendLocation(String IP,int PORT){
        this.locations.add(new DNInfo(IP,PORT));
    }

    /**
     * 获取该块的存储位置
     * @return 返回不可变列表
     */
    public List<DNInfo> getLocations(){
        return Collections.unmodifiableList(locations);
    }

}
